package arrays;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pengfei.hpf
 * @date 2020/3/5
 * @verdion 1.0.0
 * Given a sorted integer array without duplicates, return the summary of its ranges.
 *
 * Example 1:
 *
 * Input:  [0,1,2,4,5,7]
 * Output: ["0->2","4->5","7"]
 * Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
 * Example 2:
 *
 * Input:  [0,2,3,4,6,8,9]
 * Output: ["0","2->4","6","8->9"]
 * Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/summary-ranges
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class SummaryRanges {
    public List<String> summaryRanges(int[] nums){
        List<String> res = new ArrayList<>();
        if(nums == null || nums.length == 0){
            return res;
        }

        int start = nums[0];
        for(int i = 1; i < nums.length; i++){
            if(nums[i] != nums[i - 1] + 1){
                int end = nums[i - 1];
                addToRes(res, start, end);
                start = nums[i];
            }
        }
        addToRes(res, start, nums[nums.length - 1]);
        return res;
    }

    private void addToRes(List<String> res, int start, int end){
        if(start == end){
            res.add(String.valueOf(start));
        } else {
            res.add(start + "->" + end);
        }
    }

    public static void main(String[] args){

        System.out.println((new SummaryRanges()).summaryRanges(new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -1, Integer.MAX_VALUE}));
    }
}
